<?php

namespace app\spi\controller;

use app\BaseController;

class Index extends BaseController
{
    const PRIKEYPATH ='F:\php\zaozaopay\public\rsavisitor\prikey.txt';// 'F:\php\zaozaopay\public\xcxprikey.txt';//;//小程序私钥路径
    public function gateway(){
        $path=dir(getcwd())->path.'/log/spi/'.date('Ymd');
        if (! file_exists ( $path )) {
            mkdir ( "$path", 0777, true );
        }
        $address = $path."/".date('His').rand(1000,9999).'.txt';
        file_put_contents($address,file_get_contents("php://input"),FILE_APPEND);//将数据读入文本
    }

    public function test2(){
        $content=file_get_contents(self::PRIKEYPATH);
        $content=chunk_split($content, 64, "\n");
        $pem = "-----BEGIN RSA PRIVATE KEY-----\n" .$content."-----END RSA PRIVATE KEY-----";
        var_dump($pem);
        $privateKey = openssl_pkey_get_private($pem);
        var_dump($privateKey);
        if (!$privateKey) {
            die('Private key loading failed');
        }
        var_dump($privateKey);
//        $derKey = file_get_contents(self::PRIKEYPATH);
//
//        $pemKey = "-----BEGIN PRIVATE KEY-----\n" .chunk_split(base64_encode($derKey), 64, "\n") ."-----END PRIVATE KEY-----\n";
//        var_dump($pemKey);
    }
    public function test(){
        //echo phpinfo();exit;
        $path= 'F:\php\zaozaopay\public\log\spi\20250325\2137402699.txt';//小程序私钥路径2003159537 2003203053
        parse_str(file_get_contents($path),$result);
        $biz_content=$result["biz_content"];
        $info=json_decode($biz_content,true);
        echo "<pre>";
        var_dump($result);
        echo "<pre>";
        var_dump($info);

        $encryptedData = base64_decode($info["formData"]);
        $visitorInfo = base64_decode($info["visitorInfo"]);
        //var_dump($encryptedData);
        $content=file_get_contents(self::PRIKEYPATH);
        $content=chunk_split($content, 64, "\n");
        $pem = "-----BEGIN RSA PRIVATE KEY-----\n" .$content."-----END RSA PRIVATE KEY-----";
        var_dump($pem);
        $res=openssl_pkey_get_private($pem);
        var_dump($res);
        if (openssl_private_decrypt($encryptedData, $decryptedData, $pem)) {
            echo "解密成功: ";
            var_dump($decryptedData);;
        } else {
            echo "解密失败";
        }

        if (openssl_private_decrypt($visitorInfo, $decryptedData, $pem)) {
            echo "解密成功:visitorInfo ";
            var_dump($decryptedData);;
        } else {
            echo "解密失败visitorInfo";
        }
    }
}